Imports System
Imports System.IO
Imports System.Reflection
Imports CoreFX
Imports CoreFX.Interfaces
Namespace Addins
    Public Class PluginServices
        Implements CoreFX.Interfaces.IPluginManagement
        Private colAvailablePlugins As AvailablePlugins = New AvailablePlugins()
        Public Sub New()
        End Sub
        Public ReadOnly Property AvailablePlugins() As AvailablePlugins
            Get
                Return [Global].Plugins
            End Get
        End Property
        Public Sub FindPlugins()
            FindPlugins(AppDomain.CurrentDomain.BaseDirectory)
        End Sub
        Public Sub FindPlugins(ByVal Path As String) Implements Interfaces.IPluginManagement.FindLoadPlugins
            colAvailablePlugins.Clear()
            For Each fileOn As String In IO.Directory.GetFiles(Path)
                Dim file As IO.FileInfo = New IO.FileInfo(fileOn)
                If file.Name = "CoreFramework.dll" Then
                    GoTo nex1
                End If
                If file.Extension.Equals(".dll") Then
                    Me.AddPlugin(fileOn)
                End If
nex1:       Next
        End Sub
        '<Messaging.MessageSubscribe(GetType(
        Public Sub ClosePlugins()
            For Each pluginOn As AvailablePlugin In [Global].Plugins
                pluginOn.Instance.Dispose()
                pluginOn.Instance = Nothing
            Next
            [Global].Plugins.Clear()
        End Sub
        Private Sub AddPlugin(ByVal FileName As String) Implements Interfaces.IPluginManagement.AddPlugin
            Try
                Dim pluginAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(FileName)
                For Each pluginType As Type In pluginAssembly.GetTypes()
                    If pluginType.IsPublic Then
                        If Not pluginType.IsAbstract Then
                            Dim typeInterface As Type = pluginType.GetInterface("CoreFX.Addins.IPlugin", True)
                            If Not typeInterface Is Nothing Then
                                Dim newPlugin As AvailablePlugin = New AvailablePlugin()
                                newPlugin.AssemblyPath = FileName
                                newPlugin.Instance = CType(Activator.CreateInstance(pluginAssembly.[GetType](pluginType.ToString)), IPlugin)
                                newPlugin.Instance.Initialize()
                                newPlugin.Instance.Host = Me
                                newPlugin.TypeString = "ITile"
                                [Global].Plugins.Add(newPlugin)
                                newPlugin = Nothing
                                typeInterface = Nothing
                            End If
                        End If
                    End If
                Next
                pluginAssembly = Nothing
            Catch ex As Exception
                [Global].Services.ServiceInstance.Get(Of ILogging).AddItem("AddPlugin: " & ex.ToString, Logging.MessageType.General)
            End Try
        End Sub
        Public Sub Feedback(ByVal Feedback As String, ByVal Plugin As IPlugin)
            Dim newForm As System.Windows.Forms.Form = Nothing
        End Sub

        Public Sub GetRunningStatusOf(ByVal PlugName As String) Implements Interfaces.IPluginManagement.GetRunningStatusOf

        End Sub
        Public Sub Run() Implements Interfaces.IPluginManagement.Run
        End Sub
        Public Sub StopAll() Implements Interfaces.IPluginManagement.StopAll

        End Sub
    End Class
    Public Class AvailablePlugins
        Inherits System.Collections.CollectionBase
        Public Sub Add(ByVal pluginToAdd As AvailablePlugin)
            Me.List.Add(pluginToAdd)
        End Sub
        Public Sub Remove(ByVal pluginToRemove As AvailablePlugin)
            Me.List.Remove(pluginToRemove)
        End Sub
        Public Function Find(ByVal PluginName As String) As AvailablePlugin
            Dim toReturn As AvailablePlugin = Nothing
            Dim pluginOn As New AvailablePlugin
            For Each pluginOn In Me.List
                If pluginOn.Instance.Name.Equals(PluginName) Then
                    toReturn = pluginOn
                    Exit For
                End If
            Next
            Return toReturn
        End Function
        Public Function Find(ByVal pluginPath As String, ByVal UsePath As Boolean) As AvailablePlugin
            Dim toReturn As AvailablePlugin = Nothing
            Dim pluginOn As New AvailablePlugin
            For Each pluginOn In Me.List
                If pluginOn.AssemblyPath.Equals(pluginPath) Then
                    toReturn = pluginOn
                    Exit For
                End If
            Next
            Return toReturn
        End Function
    End Class
    Public Class AvailablePlugin
        Private myInstance As IPlugin = Nothing
        Private myAssemblyPath As String = ""
        Private typeStr As String = ""
        Public Property Instance() As IPlugin
            Get
                Return myInstance
            End Get
            Set(ByVal value As IPlugin)
                myInstance = value
            End Set
        End Property
     
        Public Property AssemblyPath() As String
            Get
                Return myAssemblyPath
            End Get
            Set(ByVal value As String)
                myAssemblyPath = value
            End Set
        End Property
        Public Property TypeString() As String
            Get
                Return TypeStr
            End Get
            Set(ByVal value As String)
                TypeStr = value
            End Set
        End Property
    End Class
End Namespace
